package com.hmall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auth: wan
 * @Desc: elasticsearch 配置
 * @Date: 2023/4/19 下午 4:56
 */
@Configuration
public class ElasticSearchConfig implements RestClientBuilderCustomizer {

	@Bean
	public RestHighLevelClient restHighLevelClient(){
		RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
				HttpHost.create("http://127.0.0.1:9200")
		));
		return client;
	}

	@Override
	public void customize(RestClientBuilder builder) {
		//第一个异常解决方案
		// builder.setMaxRetryTimeoutMillis(300 * 1000);
		//第二个异常解决方案
		RestClientBuilder.RequestConfigCallback configCallback = new RestClientBuilder.RequestConfigCallback() {
			@Override
			public org.apache.http.client.config.RequestConfig.Builder customizeRequestConfig(org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder) {
				return requestConfigBuilder
						.setConnectTimeout(30000)
						.setSocketTimeout(300 * 1000);//更改客户端的超时限制默认30秒现在改为5分钟
			}
		};
		builder.setRequestConfigCallback(configCallback);
	}



}
